/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tri.gui.view;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutionException;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.tri.entity.AbstractAnimal;
import org.tri.entity.AbstractCompra;
import org.tri.gui.worker.PersistenceListener;
import org.tri.gui.worker.PersistenceWorker;

/**
 *
 * @author giaco
 */
public class ComprasView extends javax.swing.JDialog
                         implements PersistenceListener{

    /**Compra con la cual se trabaja*/
    AbstractCompra compra;
    /**Modelo de lista de las caravanas*/
    DefaultListModel caravanas;
    /**Notifica un error durante la persistencia*/
    boolean errorPersist;
    
    /**
     * Creates new form ComprasView
     */
    public ComprasView( java.awt.Frame parent, boolean modal, AbstractCompra compra ){
        super( parent, modal );
        
        //asigamos la compra
        this.compra = compra;
        
        initComponents();
        
        //alineamos el dialogo
        setLocationRelativeTo( parent );
        
        //seteo todos los atributos del dialogo
        jLTipoMovimientoCompra.setText(compra.obtenerTipoMovimiento());
        SimpleDateFormat formato = new SimpleDateFormat("dd-MM-yyyy");
        jLFechaCompra.setText(formato.format(compra.obtenerFecha()));
        jLEstablecimientoCompra.setText(compra.obtenerEstablecimiento().getNombre());
        eLFTFTipoFactura.setDefaultValue(compra.getTipoFactura());
        eLFTFNumeroFactura.setDefaultValue(String.valueOf(compra.getNroFactura()));
        if(compra.getNroFactura() == 0){
            eLFTFNumeroFactura.setEnabled( false );
            eLFTFTipoFactura.setEnabled( false );
            eLFTFDtaCompra.setEnabled( true );
            eLFTFRenspaCompra.setEnabled( true );
        }
            
        eLFTFDtaCompra.setDefaultValue(String.valueOf(compra.getDtaOrigen()));
        eLFTFRenspaCompra.setDefaultValue(String.valueOf(compra.getRenspaOrigen()));
        if(compra.getDtaOrigen() == 0){
            eLFTFDtaCompra.setEnabled( false );
            eLFTFRenspaCompra.setEnabled( false );
            eLFTFTipoFactura.setEnabled( true );
            eLFTFNumeroFactura.setEnabled( true );
        }
            
        jLCantidadAnimalesCompra.setText(String.valueOf(compra.getCantidadAnimales()));
           
        caravanas = new DefaultListModel();
        for(AbstractAnimal a: compra.obtenerAnimales())
            caravanas.addElement(a);
        jLCaravanasCompra.setModel(caravanas);
            
        //deshabilito la lista para que no pueda ser modificada
        jLCaravanasCompra.setEnabled( false );
        
        this.pack();      
    
    }//end constructor

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPMQuitar = new javax.swing.JPopupMenu();
        jMIQuitar = new javax.swing.JMenuItem();
        filler7 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(32767, 0));
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 32767));
        filler5 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(32767, 0));
        filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 32767));
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLTipoMovimientoCompra = new javax.swing.JLabel();
        jLFechaCompra = new javax.swing.JLabel();
        jLEstablecimientoCompra = new javax.swing.JLabel();
        jLCantidadAnimalesCompra = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jLCaravanasCompra = new javax.swing.JList();
        eLFTFTipoFactura = new org.tri.swing.EditableLabelFTF();
        eLFTFDtaCompra = new org.tri.swing.EditableLabelFTF();
        eLFTFRenspaCompra = new org.tri.swing.EditableLabelFTF();
        eLFTFNumeroFactura = new org.tri.swing.EditableLabelFTF();
        jBAceptarCompra = new javax.swing.JButton();
        jBResetearCompra = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(32767, 0));
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));

        jMIQuitar.setText("jMenuItem1");
        jMIQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMIQuitarActionPerformed(evt);
            }
        });
        jPMQuitar.add(jMIQuitar);

        setResizable(false);
        getContentPane().add(filler7, java.awt.BorderLayout.WEST);
        getContentPane().add(filler2, java.awt.BorderLayout.NORTH);
        getContentPane().add(filler5, java.awt.BorderLayout.EAST);
        getContentPane().add(filler6, java.awt.BorderLayout.SOUTH);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel5.setText("Tipo de Movimiento:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel5, gridBagConstraints);

        jLabel6.setText("Fecha:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel6, gridBagConstraints);

        jLabel7.setText("Establecimiento:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel7, gridBagConstraints);

        jLabel8.setText("Cantidad de Animales:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel8, gridBagConstraints);

        jLabel9.setText("Caravanas:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        jPanel2.add(jLabel9, gridBagConstraints);

        jLabel10.setText("Tipo de Factura:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel10, gridBagConstraints);

        jLabel11.setText("Nro de Factura:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel11, gridBagConstraints);

        jLabel12.setText("Renspa:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel12, gridBagConstraints);

        jLabel13.setText("DTA:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        jPanel2.add(jLabel13, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(jLTipoMovimientoCompra, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(jLFechaCompra, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(jLEstablecimientoCompra, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(jLCantidadAnimalesCompra, gridBagConstraints);

        jLCaravanasCompra.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jLCaravanasCompra);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 9;
        jPanel2.add(jScrollPane2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(eLFTFTipoFactura, gridBagConstraints);
        try {
            eLFTFTipoFactura.makeEditable(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("U#")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(eLFTFDtaCompra, gridBagConstraints);
        eLFTFDtaCompra.makeEditable(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new DecimalFormat("#0"))));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(eLFTFRenspaCompra, gridBagConstraints);
        eLFTFRenspaCompra.makeEditable(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new DecimalFormat("#0"))));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel2.add(eLFTFNumeroFactura, gridBagConstraints);
        eLFTFNumeroFactura.makeEditable(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new DecimalFormat("#0"))));

        jBAceptarCompra.setText("Aceptar");
        jBAceptarCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAceptarCompraActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
        jPanel2.add(jBAceptarCompra, gridBagConstraints);

        jBResetearCompra.setText("Resetear");
        jBResetearCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBResetearCompraActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 5);
        jPanel2.add(jBResetearCompra, gridBagConstraints);

        jLabel1.setFont(org.tri.gui.view.ViewConstants.underlinedLabelFont);
        jLabel1.setText("Datos de la compra:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 11);
        jPanel2.add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel2.add(filler1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        jPanel2.add(filler3, gridBagConstraints);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jBAceptarCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAceptarCompraActionPerformed
        //me fijo si los campos editables han sido modificados
        if (eLFTFTipoFactura.isModified() || eLFTFNumeroFactura.isModified()
                || eLFTFDtaCompra.isModified() || eLFTFRenspaCompra.isModified()) {

            //seteo los campos
            compra.setTipoFactura(eLFTFTipoFactura.getText());
            if(eLFTFNumeroFactura.getText().equals(""))
                compra.setNroFactura( 0 );
            else
                compra.setNroFactura(Long.parseLong(eLFTFNumeroFactura.getText()));
            if(eLFTFDtaCompra.getText().equals(""))
                compra.setDtaOrigen( 0 );
            else
                compra.setDtaOrigen(Long.parseLong(eLFTFDtaCompra.getText()));
            if(eLFTFRenspaCompra.getText().equals(""))
                compra.setRenspaOrigen( 0 );
            else
                compra.setRenspaOrigen(Long.parseLong(eLFTFRenspaCompra.getText()));

            //almaceno la compra
            //seteo el error a false, por si ocurrio un error anteriormente
            errorPersist = false;

            //realizamos las tareas de almacenado en un thread aparte
            PersistenceWorker worker = new PersistenceWorker(compra);

            worker.addPersistenceListener( this );

            worker.execute();

        }

        this.dispose();

    }//GEN-LAST:event_jBAceptarCompraActionPerformed

    private void jBResetearCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBResetearCompraActionPerformed
        //reseteo los labels modificables al valor anterior
        eLFTFDtaCompra.resetToDefault();
        eLFTFNumeroFactura.resetToDefault();
        eLFTFRenspaCompra.resetToDefault();
        eLFTFTipoFactura.resetToDefault();

    }//GEN-LAST:event_jBResetearCompraActionPerformed

    private void jMIQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMIQuitarActionPerformed
        jLCaravanasCompra.remove(jLCaravanasCompra.getSelectedIndex());

    }//GEN-LAST:event_jMIQuitarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.tri.swing.EditableLabelFTF eLFTFDtaCompra;
    private org.tri.swing.EditableLabelFTF eLFTFNumeroFactura;
    private org.tri.swing.EditableLabelFTF eLFTFRenspaCompra;
    private org.tri.swing.EditableLabelFTF eLFTFTipoFactura;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler5;
    private javax.swing.Box.Filler filler6;
    private javax.swing.Box.Filler filler7;
    private javax.swing.JButton jBAceptarCompra;
    private javax.swing.JButton jBResetearCompra;
    private javax.swing.JLabel jLCantidadAnimalesCompra;
    private javax.swing.JList jLCaravanasCompra;
    private javax.swing.JLabel jLEstablecimientoCompra;
    private javax.swing.JLabel jLFechaCompra;
    private javax.swing.JLabel jLTipoMovimientoCompra;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuItem jMIQuitar;
    private javax.swing.JPopupMenu jPMQuitar;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    @Override
    public void donePersisting() {
        //si hay errores, mostramos el mensaje y salimos.
        if ( errorPersist ) {
            JOptionPane.showMessageDialog(null, "Error al intentar almacenar los cambios" ,
                                          "Error" , JOptionPane.ERROR_MESSAGE);
                
        }//end if
        else{
            //muestro el mensaje de que ha sido eliminado correctamente
            JOptionPane.showMessageDialog(null, "Se han almacenado los cambios satisfactoriamente" ,
                                          "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        }

     }//end método donePersisting

    @Override
    public void errorPersisting(ExecutionException ex) {
        errorPersist = true;

            //mostrar error

     }//end método errorPersisting
    
}//end clase ComprasView
